package com.lsa.letcode.the154.minInRotatedArrDupl;

import static org.junit.Assert.*;

import java.util.Arrays;

import org.junit.Test;

import com.lsa.helpers.Utils;

public class MinInRotatedArrDuplTest {
	private static final int IS_DEBUG_PRINT_N = 0;
	private static final boolean IS_DEBUG_PRINT = IS_DEBUG_PRINT_N > 0;

	@Test
	public void test() {
		testByCyclicShift(new int[] { 1, 2, 2, 3, 3, 3, 4, 4, 4, 4 }, 1);
		testByCyclicShift(new int[] { 1, 1, 1, 1, 1, 1, 3 }, 1);
		testByCyclicShift(new int[] { 1, 1, 2, 2, 3, 3 }, 1);
		testByCyclicShift(new int[] { 1, 1, 1, 3, 3, 3 }, 1);
		testByCyclicShift(new int[] { 3, 4, 4, 4, 5, 5, 1, 1, 2, 2 }, 1);
		testByCyclicShift(new int[] { 1, 1, 1, 3 }, 1);
		testByCyclicShift(new int[] { 1, 2, 3, 4, 5, 6, 7, }, 1);
		testByCyclicShift(new int[] { 1, 2, 3, 4, 5, 6, 7, 8}, 1);

		test(new int[] { 1, 1 }, 1);
		test(new int[] { 3, 1 }, 1);
		test(new int[] { 3, 3, 3 }, 3);
		test(new int[] { 1 }, 1);
		test(new int[] { 1, 2, 3, 4, 5, 6, 7, }, 1);
		test(new int[] { 6, 1, 2, 3, 4, 5 }, 1);
		test(new int[] { 6, 7, 1, 2, 3, 4, 5 }, 1);
		test(new int[] { 5, 6, 7, 1, 2, 3, 4 }, 1);
		test(new int[] { 4, 5, 6, 7, 1, 2, 3 }, 1);
		test(new int[] { 3, 4, 5, 6, 7, 1, 2 }, 1);
		test(new int[] { 2, 3, 4, 5, 6, 7, 1 }, 1);
	}
	
	private void testByCyclicShift(int[] arr, int min) {
		int[] c = arr;
		int[] first = Arrays.copyOf(arr, c.length);
		do{
			test(c,
					min);
			if (IS_DEBUG_PRINT) {System.out.println(Arrays.toString(c));}
			Utils.cyclicShiftRight(c);
		}
		while(!Arrays.equals(first, c));
	}

	private void test(int[] arr, int expectedMin) {
		Solution s = new Solution();
		int min = s.findMin(arr);
		if (IS_DEBUG_PRINT) {System.out.print(min+" ");}
		assertEquals(expectedMin, min);
	}
}
